package cn.tedu.travelsystem.order.service.impl;

import cn.tedu.travelsystem.order.pojo.entity.Order;
import cn.tedu.travelsystem.order.service.OrderService;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
@Service
public class OrderServiceImpl implements OrderService {

    private Map<Integer, Order> orders = new HashMap<>();
    private int currentId = 1;

    @Override
    public void createOrder(Order order) {
        order.setOrderId(currentId++);  // 订单ID自增赋值
        orders.put(order.getOrderId(), order);
    }

    @Override
    public boolean payOrder(int orderId) {
        Order order = orders.get(orderId);
        if (order != null && order.getOrderStatus().equals(Order.OrderStatus.UNPAID.name())) {
            order.setOrderStatus(Order.OrderStatus.COMPLETED.name()); // 标记为已完成
            return true;
        }
        return false;
    }

    @Override
    public boolean cancelOrder(int orderId) {
        Order order = orders.get(orderId);
        if (order != null && order.getOrderStatus().equals(Order.OrderStatus.UNPAID.name())) {
            order.setOrderStatus(Order.OrderStatus.CANCELED.name());
            return true;
        }
        return false;
    }

    @Override
    public Order getOrderById(int orderId) {
        return orders.get(orderId);
    }
}

